package com.xintouyun.medicine.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.xintoucloud.jdbcutil.JdbcUtil;
import com.xintouyun.medicine.dao.MenuDao_2;
import com.xintouyun.medicine.entity.Menu_2;
import com.xintouyun.medicine.model.Attributes;
import com.xintouyun.medicine.model.Node;
import com.xintouyun.medicine.service.MenuService_2;


public class MenuServiceImpl_2 implements MenuService_2{
	private MenuDao_2 nodedao=new MenuDao_2();
	@Override
	public List<Node> listNodeByParentId(String parentId) {
		try {
			List<Menu_2> menus=null;
			if(parentId==null) {
				menus=nodedao.list("select * from m_menu_2 where parentId is null");
			}else {
				menus=nodedao.list("select * from m_menu_2 where parentId=?",Integer.parseInt(parentId));
			}
			List<Node> nodes=new ArrayList<>();
			for (Menu_2 menu : menus) {
				Node node=new Node();
				node.setIconCls(menu.getIconCls());
				node.setId(menu.getId());
				node.setText(menu.getMenuname());
				Attributes attributes=new Attributes();
				attributes.setUrl(menu.getUrl());
				node.setAttributes(attributes);
				Menu_2 child=nodedao.get("select * from m_menu_2 where parentId=?", node.getId());
				if(child!=null) {
					node.setState("closed");
				}else {
					node.setState("open");
				}
				nodes.add(node);
			}
			return nodes;
		}finally {
			JdbcUtil.closeConnection();
		}
	}
	@Override
	public List<Node> listNode(String parentId) {
			try {
				List<Menu_2> menus=null;
				if(parentId==null) {
					menus=nodedao.list("select * from m_menu_2 where parentId is null");
				}else {
					menus=nodedao.list("select * from m_menu_2 where parentId=?",Integer.parseInt(parentId));
				}
				List<Node> nodes=new ArrayList<>();
				for (Menu_2 menu : menus) {
					Node node=new Node();
					node.setIconCls(menu.getIconCls());
					node.setId(menu.getId());
					node.setText(menu.getMenuname());
					Attributes attributes=new Attributes();
					attributes.setUrl(menu.getUrl());
					node.setAttributes(attributes);
					Menu_2 child=nodedao.get("select * from m_menu_2 where parentId=?", node.getId());
					if(child!=null) {
						List<Node> children=listNode(node.getId()+"");
						node.setChildren(children);
					}
					nodes.add(node);
				}
				return nodes;
			}finally {
				JdbcUtil.closeConnection();
			}
	}
}
